package com.yazi.mxz.mobile.controller;

import java.util.List;

import javax.annotation.Resource;



import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.myframework.smhj.hibernate.SearchingFactory;
import com.myframework.smhj.hibernate.Sorting;
import com.yazi.mxz.common.entity.beans.GoodsClassify;
import com.yazi.mxz.common.entity.beans.Store;
import com.yazi.mxz.common.service.GoodsClassifyService;
import com.yazi.mxz.common.service.StoreService;

@Controller("goodsClassifyController")
@RequestMapping("/goodsClassify")
public class GoodsClassifyController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(GoodsClassifyController.class);

	@Resource
	GoodsClassifyService goodsClassifyService;
	@Resource
	StoreService storeService;

	@RequestMapping
	public String list(long storeId, Long parentId, Model model) {
		log.debug("进入类目列表页面");
		Store store = storeService.one(storeId);
		if (null == store) {
			return REDIRECT_INDEX;
		}
		
		GoodsClassify classfiy4s = new GoodsClassify();// goodsClassify for search
		classfiy4s.setCompany(store.getCompany());
		
		GoodsClassify parent = null;
		
		if (null == parentId) {
			// 根类
			classfiy4s.setDeep(0);
		} else {
			// 子类
			parent = goodsClassifyService.one(parentId);
			if (null == parent) {
				return REDIRECT_INDEX;
			}
			
			classfiy4s.setGoodsClassify(parent);
		}
		
		Sorting sorting = SearchingFactory.createSorting("sort", true);
		
		List<GoodsClassify> list = goodsClassifyService.list(classfiy4s, sorting);
		
		// 没有子类了就直接跳转到相应分类的商品列表
		if (list.size() == 0) {
			return "redirect:/storeGoods/list/" + storeId
					+ (null == parentId ? "" : "?classifyId=" + parentId);
		}
		model.addAttribute("store", store);
		model.addAttribute("classify", parent);
		model.addAttribute("list", list);
		return "goodsClassify_list";
	}

}
